package dse.messages.multiversion;

import dse.DataObject;
import dse.DataRequest;
import dse.Experiment;
import dse.ExperimentRecorder;
import dse.Node;
import dse.messages.Message;
import dse.messages.MessageHandler;
import dse.messages.MessageSender;
import dse.messages.MessageType;

public class ReleaseUpdateLockMessageHandler extends MessageSender implements
		MessageHandler {
	private DataObject dobj;
	
	public ReleaseUpdateLockMessageHandler(Experiment experiment, DataObject dobj){
		super(experiment);
		this.dobj = dobj;
	}
	@Override
	public boolean acceptMessage(Message message) {
		// TODO Auto-generated method stub
		return true;
	}

	@Override
	public void messageInterupted(ExperimentRecorder recorder, Message message,
			double currentTime) {
		// TODO Auto-generated method stub
		//recorder.debug(this, "msgIntrpt: " + message, currentTime);
		//processor.lockProcess(false, message.from(), recorder, currentTime);
		;
	}

	@Override
	public void messageReceived(ExperimentRecorder recorder, Message message,
			double currentTime) {
		// TODO Auto-generated method stub
		//recorder.debug(this, "msgRcvd: " + message, currentTime);
		if (message.messageType() != MessageType.ReleaseUpdateLock) {
			recorder.warn(
					"ReleaseUpdateLockMessageHandler recieved " + 
					message.messageType() + " message."
					);
			return;
		}
		
		Node receiver = message.to();	
		DataRequest req = receiver.getNextUpdate(dobj.id());
		if(req == null){
			receiver.ReleaseUpdate(dobj.id());
		}
		else{
			Node requester = experiment.network().lookupNode(req.getClientID());
			Message m = super.createUpdateLockResponseMessage(receiver, requester, message.purpose(), req.getProcessor());
			//recorder.debug(this, "new msg: " + m, currentTime);
			experiment.messageQueue().enqueueMessage(m, currentTime);
		}
	}
}